// ==UserScript==
// @name           SL Blog Enhancement
// @namespace      http://blog.signpostmarv.name/sl-blog-enhancement/
// @description    Adds features to the SL Blog to make it easier to follow discussions
// @include        https://blogs.secondlife.com/message/*
// ==/UserScript==

function append_style(css)
{
	var head = document.getElementsByTagName('head');
	head = head[0];
	var style = document.createElement('link');
	style.rel = 'stylesheet';
	style.type = 'text/css';
	style.href = 'data:text/css,' + css;
	head.appendChild(style);
}
function quick_xpath(query)
{
	return document.evaluate(query,document,null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}
function add_section(title,node)
{
	var sidebar = document.getElementById('jive-body-sidebarcol');
	var container = document.createElement('div');
	container.className = 'jive-sidebar';
	var body = document.createElement('div');
	body.className = 'jive-sidebar-body';
	section_title = document.createElement('h4');
	section_title.appendChild(document.createTextNode(title));
	body.appendChild(section_title);
	section_contents = document.createElement('div');
	section_contents.className = 'jive-sidebar-usercontent';
	section_contents.appendChild(node);
	body.appendChild(section_contents);
	container.appendChild(body);
	sidebar.appendChild(container);
}
append_style('.sl-blog-enhancement li{list-style: none;}');
var reply_to_links = quick_xpath('*//a[starts-with(@title,"in response to: ")]');
if(reply_to_links && reply_to_links.snapshotLength && reply_to_links.snapshotLength > 0)
{
	for(var i=0;i<reply_to_links.snapshotLength;i++)
	{
		reply_to_links.snapshotItem(i).href = reply_to_links.snapshotItem(i).href.substr(reply_to_links.snapshotItem(i).href.search('#'));
	}
}
var name = quick_xpath('*//div[@id="jive-userbar-user"]/span/strong/text()');
if(name && name.snapshotLength && name.snapshotLength == 1)
{
	name = name.snapshotItem(0).nodeValue;
	var replies = quick_xpath('*//a[@title="in response to: ' + name + '"]');
	if(replies && replies.snapshotLength && replies.snapshotLength > 0)
	{
		var container = document.createElement('ol');
		container.id = 'sl-blog-enhancement-reply-tracker';
		container.className = 'sl-blog-enhancement';
		for(var i=0;i<replies.snapshotLength;i++)
		{
			var reply = document.createElement('li');
			var reply_link = document.createElement('a');
			var reply_id = replies.snapshotItem(i).parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('a')[0].name;
			reply_link.href = '#' + reply_id;
			var reply_author = replies.snapshotItem(i).parentNode.parentNode.parentNode.parentNode.getElementsByTagName('img')[0].alt;
			reply_link.appendChild(document.createTextNode(reply_id));
			reply.appendChild(reply_link);
			reply.appendChild(document.createTextNode(' (' + reply_author + ')'));
			container.appendChild(reply);
		}
		add_section('Replies to your comments',container);
	}
}
var participants = quick_xpath('*//img[@class="jive-author-avatar"]');
if(participants && participants.snapshotLength && participants.snapshotLength > 0)
{
	var participant_names = new Array();
	var participant_counts = new Array();
	var lname2name = new Array();
	for(var i=0;i<participants.snapshotLength;i++)
	{
		var participant_name = participants.snapshotItem(i).alt;
		var participant_lname = participant_name.toLowerCase();
		lname2name[participant_lname] = participant_name;
		if(participant_names.indexOf(participant_lname) < 0)
		{
			participant_names.push(participant_lname);
		}
		if(!participant_counts[participant_name])
		{
			participant_counts[participant_name] = 1;
		}
		else
		{
			participant_counts[participant_name] += 1;
		}
	}
	participant_names.sort();
	var container = document.createElement('ol');
	container.className = 'sl-blog-enhancement';
	container.id = 'sl-blog-enhancement-participants';
	for(i in participant_names)
	{
		var participant_name = lname2name[participant_names[i]];
		var participant = document.createElement('li');
		participant.appendChild(document.createTextNode(participant_name + ' (' + participant_counts[participant_name] + ')'));
		container.appendChild(participant);
	}
	add_section('Discussion Participants',container);
}